package com.pks.datacenter.service.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pks.datacenter.entity.api.ApiToken;
import com.pks.datacenter.handler.RedisHandler;
import com.pks.datacenter.mapper.api.ApiTokenMapper;
import com.pks.util.SUtilQbuilder;
import com.pks.web.mybatis.service.BasicServiceImpl;

@Component("apiTokenService")
public class ApiTokenServiceImpl extends BasicServiceImpl<ApiToken, Long, ApiTokenMapper> implements ApiTokenService {

	@Autowired
	private ApiTokenMapper apiTokenMapper;
	@Autowired
	private RedisHandler redisHandler;

	@Override
	protected ApiTokenMapper getMapper() {
		return apiTokenMapper;
	}

	@Override
	public void deleteByApiUserId(Long apiUserId) {
		apiTokenMapper.deleteByApiUserId(apiUserId);
	}

	@Override
	public ApiToken findByToken(String apiToken) {
		ApiToken apiTokenObj = redisHandler.getApiTokenByToken(apiToken);
		if (apiTokenObj == null) {
			List<ApiToken> apiList = apiTokenMapper.find(SUtilQbuilder.initQbuilder("apiToken", apiToken), 0,1,null);
			if (apiList.size() > 0) {
				apiTokenObj = apiList.get(0);
				redisHandler.setApiTokenByToken(apiToken,apiTokenObj);
			}
		}
		return apiTokenObj;
	}

	
}
